package com.guocheng.modules.dictionarie.utils;

import com.guocheng.modules.dictionarie.init.DictionariesInit;
import com.guocheng.modules.dictionarie.model.Dictionarie;

import java.util.HashMap;
import java.util.List;

public class Dictionaries extends HashMap<String, List<Dictionarie>> {

    private static Dictionaries dictionaries;

    @Override
    public synchronized List<Dictionarie> put(String key, List<Dictionarie> value) {
        return super.put(key, value);
    }

    /**
     * 获取数据字典
     * @return
     */
    public static Dictionaries getDictionaries(){
        return dictionaries;
    }

    /**
     *  根据名称获取数据字典列表
     * @param name
     * @return
     */
    public static List<Dictionarie> getDictionaries(String name){
        return dictionaries.get(name);
    }

    /**
     *  根据名称和值获取数据字典
     * @param name
     * @return
     */
    public static Dictionarie getDictionaries(String name,String value){
        List<Dictionarie> dictionaries = Dictionaries.dictionaries.get(name);
        for (Dictionarie dictionary : dictionaries) {
            if(dictionary.getValue().equals(value))
                return dictionary;
        }
        return null;
    }

    //存入数据字典
    public static void setDictionaries(Dictionaries dictionaries) {
        Dictionaries.dictionaries = dictionaries;
    }

    //重新加载数据字典
    public synchronized static boolean init(){
        System.out.println("加载数据字典····");
        DictionariesInit dictionariesInit = new DictionariesInit();
        boolean b = dictionariesInit.initDictionaries();
        if(!b){
            System.out.println("加载失败，请检查数据字典表");
        }else {
            System.out.println("加载成功！");
        }
        return b;
    }
}
